package com.qjc.demo.algorithm.删除链表重复元素;

/**
 * @ClassName: DelDuplicateNode
 * @Description: 升序排列的链表，删除所有重复的元素，使每个元素只出现一次
 * 1->1->2->3->3
 * 最终结果：1->2->3
 * @Author: qjc
 * @Date: 2022/5/6 4:21 PM
 */
public class DelDuplicateNode {

    public static DelNode delNode(DelNode head) {
        if (head == null) return null;
        DelNode currNode = head;
        while (currNode.next != null) {
            if (currNode.data == currNode.next.data) {
                currNode.next = currNode.next.next;
            } else {
                currNode = currNode.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        DelNode n1 = new DelNode(1);
        DelNode n2 = new DelNode(1);
        DelNode n3 = new DelNode(2);
        DelNode n4 = new DelNode(3);
        DelNode n5 = new DelNode(3);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = null;

        DelNode delNode = delNode(n1);
        while (delNode != null) {
            System.out.println(delNode.data);
            delNode = delNode.next;
        }
    }


}

class DelNode {
    int data;
    DelNode next;

    public DelNode(int data) {
        this.data = data;
    }
}